package com.study.zouchao.superproject.newsdetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;


import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.MyBaseFragment;
import com.study.zouchao.superproject.newsdetail.model.result.NewsDetailResult;
import com.study.zouchao.superproject.qustionadd.model.result.questionadd.QuestionAddResult;
import com.study.zouchao.superproject.util.App;
import com.study.zouchao.superproject.util.LogLongUtil;
import com.study.zouchao.superproject.util.RxSchedulers;
import com.study.zouchao.superproject.util.StringUtils;
import com.study.zouchao.superproject.util.ToastUtils;
import com.study.zouchao.superproject.util.api.api.Api;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/5/17.
 */
public class NewsDetailFragment extends MyBaseFragment {
//    @BindView(R.id.id_mapview)
//    MapView mMapView;

    @BindView(R.id.id_wv)
    WebView mWv;


    private static final String KEY_PARAM1 = "KEY_PARAM1",
            KEY_PARAM2 = "KEY_PARAM2";
    public static final String KEY_TARGET_URL = "KEY_TARGET_URL";
    private String mTargetUrl;

    public static NewsDetailFragment newInstance(String param1, String param2) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_PARAM1, param1);
        bundle.putString(KEY_PARAM2, param2);
        NewsDetailFragment instance = new NewsDetailFragment();
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        initInfos();
    }

    private void initInfos() {
        Intent intent = getActivity().getIntent();
        mTargetUrl = intent.getStringExtra(KEY_TARGET_URL);
        if (StringUtils.isEmpty(mTargetUrl)) {
            ToastUtils.showShort(App.getAppContext(), "你来晚了、该文章已被删除");
            if (getActivity() != null) getActivity().finish();
            return;
        }
        Log.d("url", mTargetUrl);
        loadData();
    }

    private void loadData() {
        Api.getInstance("https://news.baidu.com/").mApiService
                    .getNewsDetailByPOST(getRequestParams())
                    .map(RxSchedulers.<NewsDetailResult>sleep(1200))
                    .compose(RxSchedulers.<NewsDetailResult>io_main())
        .subscribe(new Action1<NewsDetailResult>() {
            @Override
            public void call(NewsDetailResult newsDetailResult) {
                ToastUtils.showShort(App.getAppContext(), "新闻详情");

                LogLongUtil.logD("新闻详情", newsDetailResult.toString());
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                throwable.printStackTrace();
                ToastUtils.showShort(App.getAppContext(), "你来晚了、该文章已被删除");
            }
        });
    }

    private Map<String, String> getRequestParams() {
        Map<String, String> params = new HashMap<>();
        params.put("src", mTargetUrl);
        return params;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_news_detail;
    }


}
